package savefile;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class FileThred implements Runnable{
    private Socket socket;
    //private List<File> fileList = null;

    public FileThred(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        //InputStream inputStream = null;
        FileOutputStream fos = null;
        InputStream is = null;
        PrintWriter pr = null;
        byte[] data = new byte[2048];
        byte[] reader = new byte[2048];
        int len;
        String content = null,path = null;
        File tempFile = null,/*newFile = null,*/file = null;
        File file1 [];
        //long startTime = System.currentTimeMillis();
        //while(true){
        try {
                //inputStream = socket.getInputStream();
                is = socket.getInputStream();
                int length = is.read(reader);
                /*if (length != -1) {
                    break;
                }*/
                path = new String(reader,0,length);
                file = new File(path);
                tempFile = new File("D:\\excel");
                if (!tempFile.exists()) {
                    tempFile.mkdirs();
                }
                //newFile = new File();//移动到指定的文件夹下
                fos = new FileOutputStream(tempFile.getAbsolutePath() + "\\" + file.getName());
                while ((len = is.read(data)) != -1){
                    fos.write(data,0,len);
                    fos.flush();
                }
               /* file1 = tempFile.listFiles();
                shoaAll(file1);*/
                pr = new PrintWriter(socket.getOutputStream(),true);
                pr.println("保存成功！");
                System.out.println("保存成功！");
        } catch (IOException e) {
            if (socket != null && !socket.isClosed()) {
                //long endTime = System.currentTimeMillis();
                System.out.println("已下线！");
                try {
                    socket.close();
                    socket = null;
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }else {
                e.printStackTrace();
            }
        } finally {
            try {
                is.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //}
    }
    /*public void shoaAll(File[] fileList1){
        PrintWriter pr = null;
        try {
            pr = new PrintWriter(socket.getOutputStream(),true);
            for (int i = 0; i < fileList1.length; i++) {
                pr.println(fileList1[i].getName());
                pr.flush();
            }
            pr.println("保存成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            pr.close();
        }

    }*/
}
